package com.zzyl.controller;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @className: RoleController
 * @description:
 * @author: InnovatorX
 * @date: 2024/6/2 9:21
 * @Version: 1.0
 */
@Slf4j
@RestController
@Api(tags = "角色管理")
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 新增角色
     * @return
     */
    @PutMapping
    @ApiOperation(value = "角色添加", notes = "角色添加")
    public ResponseResult saveRole(@RequestBody RoleDto roleDto) {
        log.info("新增角色：{}", roleDto);
        roleService.saveRole(roleDto);
        return ResponseResult.success();
    }

    /**
     * 修改角色资源关系表
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "角色修改", notes = "角色修改")
    public ResponseResult updateRole(@RequestBody RoleDto roleDto) {
        roleService.updateRole(roleDto);
        return ResponseResult.success();
    }

    /**
     * 根据角色查询选中的资源数据
     * @param roleId
     * @return
     */
    @GetMapping("/find-checked-resources/{roleId}")
    @ApiOperation(value = "根据角色查询选中的资源数据", notes = "根据角色查询选中的资源数据")
    public ResponseResult<Set<String>> findCheckedResources(@PathVariable(name = "roleId") Long roleId) {
      Set<String> resources =  roleService.findCheckedResources(roleId);
        return ResponseResult.success(resources);
    }

    @PostMapping("init-roles")
    @ApiOperation(value = "角色下拉框",notes = "角色下拉框")
    ResponseResult<List<RoleVo>> initRoles() {
        List<RoleVo> roleVoResult = roleService.initRoles();
        return ResponseResult.success(roleVoResult);
    }

    /**
     * 角色分页查询
     * @param pageNum
     * @param pageSize
     * @param roleDto
     * @return
     */
    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation(value = "角色分页", notes = "角色分页")
    public ResponseResult<PageResponse<RoleVo>> findRolePageResponse(
            @PathVariable("pageNum") int pageNum,
            @PathVariable("pageSize") int pageSize,
            @RequestBody RoleDto roleDto) {
        PageResponse<RoleVo> pageResponse =  roleService.findRolePageResponse(pageNum, pageSize, roleDto.getRoleName());
        return ResponseResult.success(pageResponse);
    }

    /**
     * 根据角色ID删除角色信息和角色中间表信息
     * @param roleIds
     * @return````
     */
    @DeleteMapping("/{roleIds}")
    @ApiOperation(value = "删除角色", notes = "删除角色")
    public ResponseResult remove(@PathVariable("roleIds") Long roleIds) {
        roleService.deleteById(roleIds);
        return ResponseResult.success();
    }
}
